import scrapy

'''
scrapy crawl ydas -o ydas.json
https://xn--0816-2jzy01cc-gu4uz87bw8o1ha9503bdc1e.avjzy0028.top/
'''

class TempadSpider(scrapy.Spider):
    name = "tempad"
    # 前缀
    prefix = 'https://xn--0816-2jzy01cc-gu4uz87bw8o1ha9503bdc1e.'
    # 中缀
    website = 'avjzy0028'
    # 后缀
    suffix = '.top/'
    allowed_domains = [website + '.top']
    start_urls = [prefix + website + suffix]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_url = get_video_url_one(content)
        tags = response.xpath("//div[@class='myvod']//p/text()").extract()
        p_url = response.xpath("//img[@class='lazy']/@ data-original").extract()
        name = response.xpath("//h4//a/text()").extract()
        url = response.xpath("//h4//a/@ href").extract()
        if len(url) and len(p_url) and len(name) and len(tags) and len(p_url) == len(name) and len(name) == len(url):
            start = tags[0].find(":") + 1
            end = tags[0].find("，", start)
            result = tags[0][start:end]
            for i in range(len(url)):
                self.i = self.i + 1
                yield get_video_item(id=self.i, tags=result, url=split_joint(self.prefix + self.website + self.suffix, url[i]), name=name[i], pUrl=p_url[i], vUrl='')
        if len(video_url):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags='', url=response.url, name='', pUrl='', vUrl=unquote(video_url[0]))
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/') and url.endswith('.html'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
